package com.example.demo.service.impl;

import com.example.demo.entity.Role;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role createRole(Role role) {
        // 检查角色名称是否已存在
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", role.getName());
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        if (!list.isEmpty()) {
            throw new BusinessException(ErrorCode.ROLE_ALREADY_EXISTS);
        }

        // 设置创建时间
        role.setCreate_time(new Date());
        role.setUpdate_time(new Date());
        
        // 插入数据
        roleMapper.insertRole(role);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        // 检查角色是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("id", role.getId());
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        if (list.isEmpty()) {
            throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
        }

        // 设置更新时间
        role.setUpdate_time(new Date());
        
        // 更新数据
        roleMapper.updateRole(role);
        return role;
    }

    @Override
    public boolean deleteRole(Integer id) {
        // 检查ID是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        if (list.isEmpty()) {
            throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
        }

        // 删除数据
        roleMapper.deleteRole(id);
        return true;
    }

    @Override
    public Map<String, Object> getRoleList(Integer page, Integer limit, String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        
        // 使用PageHelper分页
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        
        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("totalCount", pageInfo.getTotal());
        return data;
    }

    @Override
    public boolean updateRoleStatus(Integer id, Integer status) {
        // 检查ID是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        if (list.isEmpty()) {
            throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
        }

        // 更新状态
        roleMapper.updateRoleStatus(id, status);
        return true;
    }

    @Override
    public boolean setRules(Integer id, List<Integer> ruleIds) {
        // 检查ID是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Map<String, Object>> list = roleMapper.selectRoleList(params);
        if (list.isEmpty()) {
            throw new BusinessException(ErrorCode.ROLE_NOT_FOUND);
        }

        // 检查ruleIds是否为空
        if (ruleIds == null || ruleIds.isEmpty()) {
            throw new BusinessException(ErrorCode.INVALID_PARAMETER, "ruleIds不能为空");
        }

        // 更新权限
        roleMapper.updateRuleIds(id, ruleIds);
        return true;
    }

    @Override
    public List<Map<String, Object>> getAllActiveRoles() {
        Map<String, Object> params = new HashMap<>();
        params.put("status", 1);
        List<Map<String, Object>> roles = roleMapper.selectRoleList(params);
        return roles;
    }
}
